/**
 * 
 */
package org.rado.chessbatard.junit.board;

import org.rado.chessbastard.board.exceptions.InvalidBordSquareException;
import org.rado.chessbastard.board.exceptions.InvalidPieceActionException;
import org.rado.chessbastard.board.exceptions.PieceActionException;
import org.rado.chessbastard.board.objects.BoardSquare;
import org.rado.chessbastard.board.objects.IBoardSquare;
import org.rado.chessbastard.board.objects.actions.IPieceAction;
import org.rado.chessbastard.board.objects.actions.MovePieceAction;
import org.rado.chessbastard.board.objects.pieces.Rook;
import org.rado.chessbastard.board.objects.pieces.Bishop;
import org.rado.chessbastard.board.objects.pieces.IChessPiece;
import org.rado.chessbastard.board.objects.pieces.King;
import org.rado.chessbastard.board.objects.pieces.Knight;
import org.rado.chessbastard.board.objects.pieces.Pawn;
import org.rado.chessbastard.board.objects.pieces.PieceColor;
import org.rado.chessbastard.board.objects.pieces.Queen;
import org.rado.chessbatard.board.ChessBoard;
import org.rado.chessbatard.board.IChessBoard;

import junit.framework.TestCase;

/**
 * @author rado
 *
 */
public class ChessBoardTest extends TestCase {

	/**
	 * @param name
	 */
	public ChessBoardTest(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for {@link org.rado.chessbatard.board.ChessBoard#ChessBoard()}.
	 */
	public void testChessBoard() {
		IChessBoard chessBoard= new ChessBoard();
		IChessPiece[][] bord=chessBoard.getBord();
		//check 
		assertTrue("White Rook ok!",bord[0][0] instanceof Rook && bord[0][0].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Knight ok!",bord[0][1] instanceof Knight && bord[0][1].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Bishop ok!",bord[0][2] instanceof Bishop && bord[0][2].getPieceColor()==PieceColor.WHITE);
		assertTrue("White King ok!",bord[0][3] instanceof King && bord[0][3].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Queen ok!",bord[0][4] instanceof Queen && bord[0][4].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Bishop ok!",bord[0][5] instanceof Bishop && bord[0][5].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Knight ok!",bord[0][6] instanceof Knight && bord[0][6].getPieceColor()==PieceColor.WHITE);
		assertTrue("White Rook ok!",bord[0][7] instanceof Rook && bord[0][7].getPieceColor()==PieceColor.WHITE);
		for (int i=0;i<8;i++){
			assertTrue("White Pawn ok!",bord[1][i] instanceof Pawn && bord[1][i].getPieceColor()==PieceColor.WHITE);
		}
		 
		//add black pieces 
		//check 
		assertTrue("Black Rook ok!",bord[7][0] instanceof Rook && bord[7][0].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Knight ok!",bord[7][1] instanceof Knight && bord[7][1].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Bishop ok!",bord[7][2] instanceof Bishop && bord[7][2].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Queen ok!",bord[7][3] instanceof Queen && bord[7][3].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black King ok!",bord[7][4] instanceof King && bord[7][4].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Bishop ok!",bord[7][5] instanceof Bishop && bord[7][5].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Knight ok!",bord[7][6] instanceof Knight && bord[7][6].getPieceColor()==PieceColor.BLACK);
		assertTrue("Black Rook ok!",bord[7][7] instanceof Rook && bord[7][7].getPieceColor()==PieceColor.BLACK);
		for (int i=0;i<8;i++){
			assertTrue("Black Pawn ok!",bord[6][i] instanceof Pawn && bord[6][i].getPieceColor()==PieceColor.BLACK);
		}
	}
	
	/**
	 * Test method for {@link org.rado.chessbatard.board.ChessBoard#getBoard()}.
	 */
	public void testGetBoard() {
		IChessBoard board= new ChessBoard();
		assertTrue("IChessBoard getBoard returns IChessPiece array!",board.getBord() instanceof IChessPiece[][]);
		
	}
	
	/**
	 * @throws InvalidBordSquareException 
	 * Test method for {@link org.rado.chessbatard.board.ChessBoard#getPieceAt(org.rado.chessbastard.board.objects.IBoardSquare)}.
	 * @throws InvalidBordSquareException 
	 * @throws InvalidBordSquareException 
	 * @throws  
	 */
	public void testGetPieceAt() throws InvalidBordSquareException, InvalidBordSquareException  {
		IChessBoard board= new ChessBoard();
		IBoardSquare location= new BoardSquare(0,4);
		assertTrue("getPieceAt is ok!",board.getPieceAt(location) instanceof Queen);
	}

	/**
	 * @throws InvalidBordSquareException 
	 * @throws PieceActionException 
	 * @throws InvalidPieceActionException 
	 * 
	 */
	public void testExecuteAction() throws InvalidBordSquareException, InvalidPieceActionException, PieceActionException {
		IChessBoard board= new ChessBoard();
		IBoardSquare initial = new BoardSquare(1,0);
		IBoardSquare target = new BoardSquare(2,0);
		IPieceAction action = new MovePieceAction(initial,target);
		board.executeAction(action);
		assertTrue("Board action failed. Unable to move pawn!",board.getPieceAt(target) instanceof IChessPiece );	
	}

}
